<?php
/*
 * <author>Jiří Němec</author>
 * <date>2009-07-12</date>
 */

if (eregi('^(.+)/([^/]+)\.(gif|jpg|png|jpeg)$',urldecode(ereg_replace('^'.ereg_replace('[a-z]+.php$','',$_SERVER["SCRIPT_NAME"]),'',$_SERVER["REQUEST_URI"])),$out)) {
 $outputName = 'images/'.$out[2].'.'.$out[3];
 $dstExtension = $out[3];

 if (file_exists($outputName) ) {
  $size = filesize($outputName);
  if($size < 20000) {
   header('Content-Description: File Transfer');
   if (strtolower($dstExtension)=='jpg' || strtolower($dstExtension)=='jpeg') {
    header("Content-type:image/jpeg");
   } else if (strtolower($dstExtension)=='gif') {
     header("Content-type:image/gif");
    } elseif (strtolower($dstExtension)=='png') {
     header("Content-type:image/png");
    }
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   header('Content-Length: ' . $size);
   ob_clean();
   flush();
   readfile($outputName);
  } else {
   header("Status: 301"); # 301 Moved Permanently
   header("Location: ../images/".$out[2].'.'.$out[3]);
  }
 } else {
  $_SERVER["REQUEST_URI"] = ereg_replace('/read.php/','/images/',$_SERVER["REQUEST_URI"]);
  include('resize.php');
 }
}


?>